package com.sfzd5.modle;

import com.google.gson.Gson;
import com.sfzd5.exam.helplibrary.exam.Answer;
import com.sfzd5.exam.helplibrary.exam.ExamInof;
import com.sfzd5.exam.helplibrary.exam.answersheet.*;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ExamQuestionsInfo {
    public int id = 0; //
    public String examName = "答题卡测试题"; //
    public String subject = "";
    public HashMap<String, ArrayList<Question>> content;
    public Timestamp creationTime = Timestamp.valueOf(LocalDateTime.now());

    public ExamQuestionsInfo(){

    }

    @Override
    public String toString() {
        return examName;
    }

    private String getAnswerText(String answerJson){
        Answer answer = new Gson().fromJson(answerJson, Answer.class);
        return answer.content;
    }

    public ExamInof toExamInfo() {
        ExamInof info = new ExamInof();
        info.taskId = id;
        info.examName = examName;
        info.subject = subject;
        info.areaList = new ArrayList<>();
        int aid = 0;
        for (String title : content.keySet()) {
            Area area = new Area();
            area.title = title;
            area.idx = aid;
            aid++;
            ArrayList<Question> questions = content.get(title);
            if (area.title.contains("选")) {
                area.areaType = Area.AreaType.Choice;
                for (Question q : questions) {
                    ChoiceAnswerSheetQuestion cq = new ChoiceAnswerSheetQuestion();
                    cq.questionId = q.id;
                    cq.answer = getAnswerText(q.answer);
                    cq.score = 2;
                    area.answerSheetQuestionList.add(cq);
                }
            } else if (area.title.contains("填空")) {
                area.areaType = Area.AreaType.Blank;
                for (Question q : questions) {
                    BlankAnswerSheetQuestion cq = new BlankAnswerSheetQuestion();
                    cq.questionId = q.id;
                    cq.answer = getAnswerText(q.answer);
                    cq.score = 2;
                    List<BlankInfo> blankInfoList = new ArrayList<>();
                    int k = 4;
                    if(cq.answer!=null && !cq.answer.isEmpty()){
                        k = countString(cq.answer, "<br>");
                        if(k==0) k = 1;
                    }
                    for(int i=0; i<k; i++) {
                        blankInfoList.add(new BlankInfo());
                    }
                    cq.setBlankInfoList(blankInfoList);
                    area.answerSheetQuestionList.add(cq);
                }
            } else {
                area.areaType = Area.AreaType.Expound;
                for (Question q : questions) {
                    ExpoundAnswerSheetQuestion cq = new ExpoundAnswerSheetQuestion();
                    cq.questionId = q.id;
                    cq.answer = getAnswerText(q.answer);
                    cq.score = 2;
                    List<ExpoundChildQuestion> childQuestionList = new ArrayList<>();
                    if (q.child != null) {
                        for (int i = 0; i < q.child.size(); i++) {
                            childQuestionList.add(new ExpoundChildQuestion());
                        }
                    } else {
                        childQuestionList.add(new ExpoundChildQuestion());
                    }
                    cq.setChildQuestionList(childQuestionList);
                    area.answerSheetQuestionList.add(cq);
                }
            }
            info.areaList.add(area);
        }
        return info;
    }

    public static int countString(String str, String s) {
        String[] ss = str.split(s);
        return ss.length;
    }
}
